package com.eastfair.home.controller;

import com.eastfair.core.exception.BizException;
import com.eastfair.home.dto.*;
import com.eastfair.home.entity.ReportServiceAudit;
import com.eastfair.home.entity.ReportServiceAuditHistory;
import com.eastfair.home.entity.ReportSpaceAudit;
import com.eastfair.home.exceptioncode.HomeExceptionCode;
import com.eastfair.home.service.ReportServiceAuditService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.home.vo.ReportEarnestMoneyAuditVO;
import com.eastfair.home.vo.ReportServiceAuditVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;

import javax.validation.constraints.NotEmpty;


/**
 * <p>
 * 前端控制器
 * 报馆服务审核记录
 * </p>
 *
 * @author linan
 * @date 2022-08-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportServiceAudit")
@Api(value = "ReportServiceAudit", tags = "报馆服务审核记录")
@PreAuth(replace = "home:reportServiceAudit:")
public class ReportServiceAuditController extends SuperController<ReportServiceAuditService, Long, ReportServiceAudit, ReportServiceAuditPageQuery, ReportServiceAuditSaveDTO, ReportServiceAuditUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<ReportServiceAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ReportServiceAudit> reportServiceAuditList = list.stream().map((map) -> {
            ReportServiceAudit reportServiceAudit = ReportServiceAudit.builder().build();
            //TODO 请在这里完成转换
            return reportServiceAudit;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(reportServiceAuditList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportServiceAuditPageQuery> params) {
        ReportServiceAuditPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 保存预订服务审核数据 （搭建商调用）
     * @param reportServiceAuditList reportServiceAuditList
     * @return R<Boolean>
     */
    @PostMapping("/saveReportServiceAudit")
    public R<Boolean> saveReportServiceAudit(@RequestBody List<ReportServiceAuditDTO> reportServiceAuditList){
        if (CollectionUtils.isEmpty(reportServiceAuditList)) {
            throw BizException.wrap(HomeExceptionCode.SERVICE_CANT_EMPTY);
        }
        return R.success(baseService.saveReportServiceAudit(reportServiceAuditList));
    }

    /**
     * 提交服务审核
     * @param reportSpaceAuditDTO reportSpaceAuditDTO
     * @return R<Boolean>
     */
    @PostMapping("/submitServiceAudit")
    public R<Boolean> submitServiceAudit(@RequestBody ReportSpaceAuditDTO reportSpaceAuditDTO){
        return R.success(baseService.submitServiceAudit(reportSpaceAuditDTO));
    }

    /**
     * 保存服务审核
     * @param reportServiceAudits reportServiceAudits
     * @return R<Boolean>
     */
    @PostMapping("/saveServiceAudit")
    public R<Boolean> saveServiceAudit(@RequestBody List<ReportServiceAuditDTO> reportServiceAudits){
        return R.success(baseService.saveServiceAudit(reportServiceAudits));
    }

    /**
     * 查询服务审核列表
     * @param reportSpaceAuditDTO reportSpaceAuditDTO
     * @return R<Boolean>
     */
    @PostMapping("/queryReportServiceAuditList")
    public R<List<ReportServiceAuditVO>> queryReportServiceAuditList(@RequestBody ReportSpaceAuditDTO reportSpaceAuditDTO){
        return R.success(baseService.queryReportServiceAuditList(reportSpaceAuditDTO));
    }

    /**
     * 查询保证金审核列表
     * @param reportSpaceAuditDTO reportSpaceAuditDTO
     * @return R<Boolean>
     */
    @PostMapping("/queryReportEarnestMoneyAudit")
    public R<ReportEarnestMoneyAuditVO> queryReportEarnestMoneyAudit(@RequestBody ReportSpaceAuditDTO reportSpaceAuditDTO){
        return R.success(baseService.queryReportEarnestMoneyAudit(reportSpaceAuditDTO));
    }

    /**
     * 更新未生成订单的报馆服务审核记录
     * @param reportServiceAuditDTOS reportServiceAuditDTOS
     * @return R<Boolean>
     */
    @PostMapping("/updateByItem")
    public R<Boolean> updateByItem(@RequestBody List<ReportServiceAuditDTO> reportServiceAuditDTOS){
        return R.success(baseService.updateByItem(reportServiceAuditDTOS));
    }

    /**
     * 删除当前报馆服务审核
     * @param reportSpaceId reportSpaceId
     * @return R<Boolean>
     */
    @GetMapping(value = "/delServiceAuditByReportSpaceId")
    public R<Boolean> delServiceAuditByReportSpaceId(@RequestParam("reportSpaceId") Long reportSpaceId) {
        return R.success(baseService.delServiceAuditByReportSpaceId(reportSpaceId));
    }

}
